/**
 * @brief:  ros topic 下发
 * @author: haitao.lin
 */
package websocket

import (
	"iron_man_comm/src/common/log"
	"iron_man_comm/src/goroslib/pkg/msgs/std_msgs"
	"iron_man_comm/src/msg/common_msg"
	"time"
)

type ControlCleanMotor struct {
	Cmd  string                `json:"cmd"`
	Data ControlCleanMotorData `json:"data"`
}

type ControlCleanMotorData struct {
	//控制标识位，true表示控制有效，false表示不控制
	WindFlag      bool `json:"windFlag"`
	EdgeBrushFlag bool `json:"edgeBrushFlag"`
	RollBrushFlag bool `json:"rollBrushFlag"`
	LeftMopFlag   bool `json:"leftMopFlag"`
	RightMopFlag  bool `json:"rightMopFlag"`
	WaterPumpFlag bool `json:"waterPumpFlag"`
	//电机属性，只有标识为true时有效
	WindGear         uint16 `json:"windGear"`
	EdgeBrushVoltage int16  `json:"edgeBrushVoltage"`
	RollBrushVoltage int16  `json:"rollBrushVoltage"`
	LeftMopVoltage   int16  `json:"leftMopVoltage"`
	LeftMopRunTime   uint8  `json:"leftMopRunTime"`
	RightMopVoltage  int16  `json:"rightMopVoltage"`
	RightMopRunTime  uint8  `json:"rightMopRunTime"`
	WaterPump        uint16 `json:"waterPump"`
}

/**
 * @brief:  控制清洁电机
 * @author: haitao.lin
 */
func (s *IotServer) controlCleanMotor(cmd ControlCleanMotor) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control clean motor publish topic, control data: %v", cmd)

		msg := &common_msg.ControlCleanDevice{
			Header: std_msgs.Header{
				Stamp: time.Now(),
			},
			WindFlag:         cmd.Data.WindFlag,
			EdgeBrushFlag:    cmd.Data.EdgeBrushFlag,
			RollBrushFlag:    cmd.Data.RollBrushFlag,
			LeftMopFlag:      cmd.Data.LeftMopFlag,
			RightMopFlag:     cmd.Data.RightMopFlag,
			WaterPumpFlag:    cmd.Data.WaterPumpFlag,
			WindGear:         cmd.Data.WindGear,
			EdgeBrushVoltage: cmd.Data.EdgeBrushVoltage,
			RollBrushVoltage: cmd.Data.RollBrushVoltage,
			LeftMopVoltage:   cmd.Data.LeftMopVoltage,
			LeftMopRunTime:   cmd.Data.LeftMopRunTime,
			RightMopVoltage:  cmd.Data.RightMopVoltage,
			RightMopRunTime:  cmd.Data.RightMopRunTime,
			WaterPump:        cmd.Data.WaterPump,
		}
		publisher.Write(msg)
	} else {
		log.Error("control clean motor publisher cannot be found")
	}
}

type ControlExtendMotor struct {
	Cmd  string                 `json:"cmd"`
	Data ControlExtendMotorData `json:"data"`
}

type ControlExtendMotorData struct {
	RollBrushExtendFlag bool  `json:"rollBrushExtendFlag"`
	EdgeBrushExtendFlag bool  `json:"edgeBrushExtendFlag"`
	MopExtendFlag       bool  `json:"mopExtendFlag"`
	RollBrushCmd        uint8 `json:"rollBrushCmd"`
	EdgeBrushCmd        uint8 `json:"edgeBrushCmd"`
	MopCmd              uint8 `json:"mopCmd"`
}

/**
 * @brief:  控制外摆电机
 * @author: haitao.lin
 */
func (s *IotServer) controlExtendMotor(cmd ControlExtendMotor) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control extend motor publish topic, control data: %v", cmd)

		msg := &common_msg.ControlExtendDevice{
			Header: std_msgs.Header{
				Stamp: time.Now(),
			},
			RollBrushExtendFlag: cmd.Data.RollBrushExtendFlag,
			EdgeBrushExtendFlag: cmd.Data.EdgeBrushExtendFlag,
			MopExtendFlag:       cmd.Data.MopExtendFlag,
			RollBrushCmd:        cmd.Data.RollBrushCmd,
			EdgeBrushCmd:        cmd.Data.EdgeBrushCmd,
			MopCmd:              cmd.Data.MopCmd,
		}
		publisher.Write(msg)
	} else {
		log.Error("control extend motor publisher cannot be found")
	}
}

type ControlLight struct {
	Cmd  string           `json:"cmd"`
	Data ControlLightData `json:"data"`
}

type ControlLightData struct {
	Value uint8 `json:"value"`
}

/**
 * @brief:  控制灯效
 * @author: haitao.lin
 */
func (s *IotServer) controlLight(cmd ControlLight) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control light publish topic, control data: %v", cmd)

		msg := &common_msg.ControlLight{
			Header: std_msgs.Header{
				Stamp: time.Now(),
			},
			Cmd: cmd.Data.Value,
		}
		publisher.Write(msg)
	} else {
		log.Error("control light publisher cannot be found")
	}
}

type ControlSensor struct {
	Cmd  string            `json:"cmd"`
	Data ControlSensorData `json:"data"`
}

type ControlSensorData struct {
	//传感器标识位,true表示控制有效，false表示不控制
	DownViewFlag   bool `json:"downViewFlag"`   //下视
	UltrasonicFlag bool `json:"ultrasonicFlag"` //超声
	ChargeFlag     bool `json:"chargeFlag"`     //充电
	//控制类型，对应标志位为true时有效
	DownViewSwitch   uint8 `json:"downViewSwitch"`
	UltrasonicSwitch uint8 `json:"ultrasonicSwitch"`
	ChargeSwitch     uint8 `json:"chargeSwitch"`
}

/**
 * @brief:  控制传感器
 * @author: haitao.lin
 */
func (s *IotServer) controlSensor(cmd ControlSensor) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control sensor publish topic, control data: %v", cmd)

		msg := &common_msg.ControlSensorSwitch{
			Header:           std_msgs.Header{},
			DownViewFlag:     cmd.Data.DownViewFlag,
			UltrasonicFlag:   cmd.Data.UltrasonicFlag,
			ChargeFlag:       cmd.Data.ChargeFlag,
			DownViewSwitch:   cmd.Data.DownViewSwitch,
			UltrasonicSwitch: cmd.Data.UltrasonicSwitch,
			ChargeSwitch:     cmd.Data.ChargeSwitch,
		}
		publisher.Write(msg)
	} else {
		log.Error("control sensor publisher cannot be found")
	}
}

type ControlMcuPower struct {
	Cmd  string              `json:"cmd"`
	Data ControlMcuPowerData `json:"data"`
}

type ControlMcuPowerData struct {
	Cmd uint8 `json:"cmd"`
}

/**
 * @brief:  控制MCU上下电
 * @author: haitao.lin
 */
func (s *IotServer) controlMCUPower(cmd ControlMcuPower) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control mcu power publish topic, control data: %v", cmd)

		msg := &common_msg.ControlPower{
			Header: std_msgs.Header{
				Stamp: time.Now(),
			},
			Cmd: cmd.Data.Cmd,
		}
		publisher.Write(msg)
	} else {
		log.Error("control mcu power publisher cannot be found")
	}
}

type ControlCleanMotorSpeed struct {
	Cmd  string                     `json:"cmd"`
	Data ControlCleanMotorSpeedData `json:"data"`
}

type ControlCleanMotorSpeedData struct {
	//控制标识位，true表示控制有效，false表示不控制
	WindFlag      bool `json:"windFlag"`
	EdgeBrushFlag bool `json:"edgeBrushFlag"`
	RollBrushFlag bool `json:"rollBrushFlag"`
	LeftMopFlag   bool `json:"leftMopFlag"`
	RightMopFlag  bool `json:"rightMopFlag"`
	//电机属性，只有标识为true时有效
	WindGearSpeed  uint16 `json:"windGearSpeed"`
	EdgeBrushSpeed int16  `json:"edgeBrushSpeed"`
	RollBrushSpeed int16  `json:"rollBrushSpeed"`
	LeftMopSpeed   int16  `json:"leftMopSpeed"`
	LeftMopTime    uint8  `json:"leftMopTime"`
	RightMopSpeed  int16  `json:"rightMopSpeed"`
	RightMopTime   uint8  `json:"rightMopTime"`
}

func (s *IotServer) ControlCleanMotorSpeed(cmd ControlCleanMotorSpeed) {
	publisher, ok := s.publisherMap[cmd.Cmd]
	if ok {
		log.Info("control clean motor speed publish topic, control data: %v", cmd)

		msg := &common_msg.ControlCleanDeviceSpeed{
			Header: std_msgs.Header{
				Stamp: time.Now(),
			},
			WindFlag:       cmd.Data.WindFlag,
			EdgeBrushFlag:  cmd.Data.EdgeBrushFlag,
			RollBrushFlag:  cmd.Data.RollBrushFlag,
			LeftMopFlag:    cmd.Data.LeftMopFlag,
			RightMopFlag:   cmd.Data.RightMopFlag,
			WindGearSpeed:  cmd.Data.WindGearSpeed,
			EdgeBrushSpeed: cmd.Data.EdgeBrushSpeed,
			RollBrushSpeed: cmd.Data.RollBrushSpeed,
			LeftMopSpeed:   cmd.Data.LeftMopSpeed,
			LeftMopTime:    cmd.Data.LeftMopTime,
			RightMopSpeed:  cmd.Data.RightMopSpeed,
			RightMopTime:   cmd.Data.RightMopTime,
		}
		publisher.Write(msg)
	} else {
		log.Error("control clean motor speed publisher cannot be found")
	}
}
